package com.zyb.patient.activity;
import java.util.LinkedHashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.zyb.patient.IBaseCallBack;
import com.zyb.patient.R;
import com.zyb.patient.ZybApplication;
import com.zyb.patient.dto.UserDto;
import com.zyb.patient.service.DbSqliteService;
import com.zyb.patient.service.MainService;
import com.zyb.patient.service.Task;
import com.zyb.patient.util.StringUtil;
import com.zyb.patient.util.ToastUtil;
import com.zyb.patient.view.AboutLoadingdialog;

public abstract class BaseActivity extends FragmentActivity implements IBaseCallBack{

	/*** 初始化Activity ***/
	protected AboutLoadingdialog dialog;
	protected Toast toast;
	protected Context mContext;
	protected DbSqliteService db;


	@SuppressLint("ShowToast")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		MainService.addActivity(this);
		super.onCreate(savedInstanceState);
		mContext = ZybApplication.getContext();
		db = new DbSqliteService(mContext);
		dialog = new AboutLoadingdialog(this);
		toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);

	}

	@Override
	protected void onStop() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
		super.onStop();
	}
	
	/** 获取当前用户 **/
	public UserDto getcurrentUser() {
		String uid = db.getConfigItem(DbSqliteService.USER_ID);
		Log.e("luoming uid",uid+"");
		if (!"0".equals(uid))
			return db.selectOneUserDto(uid);
		return null;
	}
	
	/** 数据访问 **/
	@Override
    public void refresh(Object... param) {
		
	}
	
	@SuppressWarnings("rawtypes")
	public void getData(LinkedHashMap map, int TaskId, int source)// 0:loading
	{

		if (source != 0 && !dialog.isShowing()) {
			dialog.show();
		}
		Log.e("luoming", map + "");
		Task task = new Task(TaskId, map);
		MainService.newTask(task);

	}

	@SuppressWarnings("rawtypes")
	public void getData(LinkedHashMap map, int TaskId) {
		if (!dialog.isShowing()) {
			dialog.show();
		}
		Task task = new Task(TaskId, map);
		MainService.newTask(task);
	}
	
	/** 弹出Toast **/
	public void showToast(int msg) {
		ToastUtil.showMessage(mContext, msg, Toast.LENGTH_SHORT);

	}

	public void showToast(String msg) {
		ToastUtil.showMessage(mContext, msg, Toast.LENGTH_SHORT);

	}

	public void colseDialog() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
	}
	
	/************* 退出 **************/
	// 软按键
	public void onclickBack(View paramView) {
		super.onBackPressed();
	}

	// 硬按键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			onBackPressed();
			return false;
		}
		return false;
	}

	public void onBackPressed() {
		if (this instanceof MainActivity) {
			((MainActivity) this).exitBy2Click();
		} else {
			finish();
		}
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.noanim, R.anim.exit_leftttoight);
	}



}